/*
 *
 *过滤器
 */

import moment from 'moment';
import accounting from 'accounting';

export default {
  phoneFilter: function(val) {
    return val.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  },
  strTonum: function(val) {
    return parseInt(val);
  },
  toFixed: function(val) {
    return accounting.toFixed(val, 4);
  },
  formatDate: function(value, formatString) {
    formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
    return moment(value).format(formatString);
  },
  diffTime: function(s) {
    // var _startDate = new Date(startDate);
    // var _endDate = new Date(endDate);
    // var diff = _endDate.getTime() - _startDate.getTime();// 时间差的毫秒数
    // // 计算出相差天数
    // // var days = Math.floor(diff / (24 * 3600 * 1000));
    // // 计算出小时数
    // var leave1 = diff % (24 * 3600 * 1000); // 计算天数后剩余的毫秒数
    // var hours = Math.floor(leave1 / (3600 * 1000));
    // // 计算相差分钟数
    // var leave2 = leave1 % (3600 * 1000); // 计算小时数后剩余的毫秒数
    // var minutes = Math.floor(leave2 / (60 * 1000));

    // // 计算相差秒数
    // var leave3 = leave2 % (60 * 1000); // 计算分钟数后剩余的毫秒数
    // var seconds = Math.round(leave3 / 1000);

    // var returnStr = (seconds || 0) + '秒';
    // if (minutes > 0) {
    //   returnStr = minutes + '分' + returnStr;
    // }
    // if (hours > 0) {
    //   returnStr = hours + '小时' + returnStr;
    // }
    // return returnStr;
    var _s = parseInt(s);
    var h;
    h = Math.floor(_s / 60);
    _s = _s % 60;
    h += '';
    s += '';
    _s = (_s.length === 1) ? '0' + _s : _s;
    return h + '分' + _s + '秒';
  }
};
